package com.rrx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Controller
public class PageController {

    @RequestMapping("/")
    public String welcome() {
        return "login";
    }

    @RequestMapping("/index")
    public String toIndex() {
        return "index";
    }

    @RequestMapping("/getImg")
    @ResponseBody
    public String getImg(HttpServletResponse response) {
        response.setHeader("Content-Type","image/jped");
        BufferedImage img = new BufferedImage(60, 60, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = img.createGraphics();
        g.setColor(Color.red);
        g.fillRect(0, 0, 60, 60);
        Image image = img;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(img, "jpg", baos);
            byte[] bytes = baos.toByteArray();
            BASE64Encoder encoder = new sun.misc.BASE64Encoder();
            return encoder.encodeBuffer(bytes).trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("toAddAdmin")
    public String toAdmin() {
        return "view/addAdmin";
    }

    @RequestMapping("toAddMenu")
    public String toMenu() {
        return "view/addMenu";
    }

    @RequestMapping("toAddRole")
    public String toRole() {
        return "view/addRole";
    }

    @RequestMapping("toUpdateAdmin")
    public String toUpdateAdmin() {
        return "view/updateAdmin";
    }

    @RequestMapping("toUpdateRole")
    public String toUpdateRole() {
        return "view/role/updateRole";
    }

    @RequestMapping("toAuthFettler")
    public String toAuthFettler() {return "view/authFettler"; }

    @RequestMapping("toAddFault")
    public String toAddFault() {return "view/addFault";}

    @RequestMapping("toUpdateFault")
    public String toUpdateFault() {return "view/updateFault";}

    @RequestMapping("toSelectOrders")
    public String toSelectOrders() {return "view/orders/selectOrders";}
}
